//
//  KeyBoardUtils.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/11.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit

class KeyBoardUtils: NSObject {
    static let shared:KeyBoardUtils = {
        let util = KeyBoardUtils()
        return util
    }()
    
    //键盘将要显示时调用的通知方法
    func keyBoardWillShow(_ note:Notification , showView:UIView , finished:(_ keyBoardHeight:CGFloat) -> Void){
        let userInfo:NSDictionary  = (note as NSNotification).userInfo! as NSDictionary
        let  keyBoardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey]as! NSValue).cgRectValue
        let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        let deltaY = keyBoardBounds.size.height
        let animations:(() -> Void) = {
            showView.transform = CGAffineTransform(translationX: 0,y: -(deltaY))
        }
        if duration > 0 {
            let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
            UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
        }else{
            animations()
        }
        
        finished(deltaY)
        
    }
    //键盘将要隐藏时调用的通知方法
    func keyBoardWillHide(_ note:Notification , hideView:UIView , finished:() -> Void){
        let userInfo  = (note as NSNotification).userInfo!
        let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
        let animations:(() -> Void) = {
            hideView.transform = CGAffineTransform.identity
        }
        if duration > 0 {
            let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
            UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
        }else{
            animations()
        }
        
        finished()
    }
    
    func keyBoardWillChangeFrame(_ note:Notification , finished:(_ keyBoardHeight:CGFloat) -> Void){
        let userInfo:NSDictionary  = (note as NSNotification).userInfo! as NSDictionary
        let  keyBoardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey]as! NSValue).cgRectValue
        let deltaY = keyBoardBounds.size.height
        finished(deltaY)
    }
    
}
